<template>
  <div>
    <el-form :inline="true" ref="searchForm" :model="searchMap" class="demo-form-inline">
      <el-form-item prop="cardNum">
        <el-input v-model="searchMap.cardNum" placeholder="会员卡号"></el-input>
      </el-form-item>
      <el-form-item prop="name">
        <el-input v-model="searchMap.name" placeholder="会员名称"></el-input>
      </el-form-item>
      <el-form-item prop="payType">
        <el-select v-model="searchMap.payType" placeholder="支付类型">
          <el-option
            v-for="item in payTypeOptions"
            :key="item.type"
            :label="item.name"
            :value="item.type"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item prop="birthday">
        <el-date-picker
          value-format="yyyy-MM-dd"
          v-model="searchMap.birthday"
          type="date"
          placeholder="出生日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" size="small" @click="getdatalista">查询</el-button>
        <el-button type="primary" size="small" @click="dialogFormVisible = true">新增</el-button>
        <el-button size="small" @click="resetForm('searchForm')">重置</el-button>
      </el-form-item>
    </el-form>

    <!--  数据列表  -->
    <el-table :data="list" style="width: 100%" height="380">
      <el-table-column type="index" label="序号" width="60"></el-table-column>
      <el-table-column prop="cardNum" label="会员卡号" width="160"></el-table-column>
      <el-table-column prop="name" label="会员姓名"></el-table-column>
      <el-table-column prop="phone" label="手机号码" width="120"></el-table-column>
      <el-table-column prop="integral" label="可用积分"></el-table-column>
      <el-table-column prop="money" label="开卡金额"></el-table-column>
      <el-table-column prop="payType" label="支付类型">
        <template slot-scope="scope">{{scope.row.payType | paytyepefilter}}</template>
      </el-table-column>
      <el-table-column prop="address" label="会员地址" width="190"></el-table-column>
      <el-table-column label="操作" width="150">
        <el-button size="mini" @click="onclick">编辑</el-button>
        <el-button size="mini" type="danger" @click="handledelete">删除</el-button>
      </el-table-column>
    </el-table>

    <!-- 数据翻页 -->
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleChange"
      :current-page="currentPage"
      :page-sizes="[10, 20, 50]"
      :page-size="pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
    ></el-pagination>

    <!-- 新增表单 -->
    <el-dialog title="会员新增" :visible.sync="dialogFormVisible" width="30%">
      <el-form :model="pojo" :rules="rules" ref="pojoForm">
        <el-form-item label="会员卡号" prop="cardNum" :label-width="formLabelWidth">
          <el-input v-model="pojo.cardNum" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="会员姓名" prop="name" :label-width="formLabelWidth">
          <el-input v-model="pojo.name" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="会员生日" prop="birthday" :label-width="formLabelWidth">
          <el-date-picker
            value-format="yyyy-MM-dd"
            v-model="pojo.birthday"
            type="date"
            placeholder="出生日期"
          ></el-date-picker>
        </el-form-item>
        <el-form-item label="手机号码" prop="phone" :label-width="formLabelWidth">
          <el-input v-model="pojo.phone" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="开卡金额" prop="money" :label-width="formLabelWidth">
          <el-input v-model="pojo.money" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="可用金额" :label-width="formLabelWidth">
          <el-input v-model="pojo.integral" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="支付类型" :label-width="formLabelWidth">
          <el-select v-model="pojo.payType" placeholder="支付类型">
            <el-option
              v-for="item in payTypeOptions"
              :key="item.type"
              :label="item.name"
              :value="item.type"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="会员地址" label-width="120px">
          <el-input type="textarea" v-model="pojo.desc" label-width="50px"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button
          type="primary"
          @click="pojo.id===null?addData('pojoForm'):updateclck('pojoForm')"
        >确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import member from "../api/member";
const payTypeOptions = [
  {
    type: "1",
    name: "现金"
  },
  {
    type: "2",
    name: "支付宝"
  },
  {
    type: "3",
    name: "微信"
  },
  {
    type: "4",
    name: "银行卡"
  }
];

export default {
  data() {
    return {
      list: [],
      total: 0,
      pageSize: 10,
      currentPage: 1,
      searchMap: {
        cardNum: "",
        name: "",
        payType: "",
        birthday: ""
      },
      payTypeOptions,
      dialogTableVisible: false,
      dialogFormVisible: false,
      formLabelWidth: "120px",
      pojo: {
        id: null,
        cardNum: "",
        name: "",
        birthday: "",
        phone: "",
        money: 0,
        integral: 0,
        payType: "",
        address: ""
      },
      rules: {
        cardNum: [{ required: true, message: "卡号不能为空", trigger: "blur" }],
        name: [{ required: true, message: "姓名不能为空", trigger: "blur" }],
        payType: [
          { required: true, message: "请选择支付类型", trigger: "change" }
        ]
      }
    };
  },
  mounted() {
    this.getdatalista();
  },
  methods: {
    addData(formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          member.add(this.pojo).then(res => {
            // window.console.log(res)
            const resp = res.data;
            if (resp.flag) {
              this.$refs[formName].resetFields();
              this.getdatalista();
              this.dialogFormVisible = false;
            }
          });
          // 验证通过，提交添加
          alert("Add submit!");
        } else {
          // 验证不通过
          return false;
        }
      });
    },

    //获取列表接口数据
    getdatalista() {
      member.getdatalist(this.currentPage, this.pageSize, this.searchMnp).then(res => {
          const resp = res.data;
          this.list = resp.data.rows;
          //  获取列表数据页数
          this.total = resp.data.total;
          // window.console.log(resp)
        });
    },
    // 设置翻页方法
    handleChange(value) {
      // window.console.log('设置翻页方法')
      this.currentPage = value;
      this.getdatalista();
    },
    // 设置每一页显示条数方法
    handleSizeChange(value) {
      //  window.console.log("11111111")
      this.pageSize = value;
      this.getdatalista();
    },
    // 重置数据方法
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
    deleteitem(item) {
      //  window.console.log(item)
      this.list.splice(item, 1);
    },

    // 获取用户ID
    onclick(id) {
      member.setitem(id).then(res => {
        this.dialogFormVisible = true;
        const setoid = res.data;
        window.console.log(setoid.data);
        if (setoid.flag) {
          this.pojo = setoid.data;
        }
      });
    },
    // 更新数据
    updateclck(formName) {
      this.$refs[formName].validate(valid => {
        // window.console.log(valid)
        if (valid) {
          member.updete(this.pojo).then(res => {
            const updateitem = res.data;
            if (updateitem.flag) {
              // 重新调用接口
              this.getdatalista();
              this.dialogFormVisible = false;
            } else {
              this.$message({
                message: updateitem.message,
                type: "warning"
              });
            }
          });
        } else {
          return false;
        }
      });
    },

      // 删除
      handledelete(id){
          this.$confirm('确认删除这条记录吗?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning',
          center: true
        }).then(() => {
         member.setdelete(id).then((res)=>{
          const resp =res.data
          this.$message({
            type: resp.flag?'success':"error",
            message: resp.message
          });
           if(resp.flag){
             this.getdatalista();
           }
         })
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          });
        });
      }
  },
  // 筛选数据内的支付方式
  filters: {
    paytyepefilter(type) {
      //  var temp=''
      const pay = payTypeOptions.find(data => {
        return data.type == type;
      });
      // window.console.log(pay)

      return pay ? pay.name : null;
    }
  }
};
</script>

<style>
</style>